#include "userchange.h"
#include "ui_userchange.h"

#include <QMessageBox>
#include <QSqlTableModel>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include <QSqlQuery>

#include "mylibrary.h"
#include "user.h"

UserChange::UserChange(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserChange)
{
    ui->setupUi(this);
}

UserChange::~UserChange()
{
    delete ui;
}

void UserChange::on_pushButton_2_clicked()
{
    close();
}

void UserChange::on_pushButton_clicked()
{
    if(ui->lineEditAccount_Personal->text().isEmpty() ||
            ui->lineEditEmail->text().isEmpty() ||
            ui->lineEditPassword_before->text().isEmpty() ||
            ui->lineEditPassword_after->text().isEmpty() ||
            ui->lineEditPassword_confirm->text().isEmpty() ||
            ui->lineEditPhone->text().isEmpty() ||
            !(ui->radioButtonMan->isChecked() || ui->radioButtonWoman->isChecked()))
    {
        QMessageBox:: critical(this, "Error", "Please input completely!");
    }
    else
    {
        if(ui->lineEditPassword_after->text() == ui->lineEditPassword_confirm->text())
        {
            QSqlQuery query;
            query.prepare("INSERT INTO user(Account, Password, Sex, Phone, Email, Authorization)"
                          "VALUES(:Account, :Password, :Sex, :Phone, :Email, :Authorization)");
            query.bindValue(":Account", ui->lineEditAccount_Personal->text());
            query.bindValue(":Password", ui->lineEditPassword_confirm->text());
            query.bindValue(":Sex", NULL);
            query.bindValue(":Phone", ui->lineEditPhone->text());
            query.bindValue(":Email", ui->lineEditEmail->text());
            query.bindValue(":Authorization", 0);
            bool ok = query.exec();
            if(!ok)
            {
                QMessageBox:: critical(this, "Error", "Insert Error");
            }
            else
            {
                qDebug() << "数据更新成功";
                myLibrary *u_l = new myLibrary();
                u_l->show();
                close();

            }
        }
        else
        {
            QMessageBox::critical(this,"Error", "前后密码不一致");
        }
    }
}

void UserChange::on_pushButtonReturnuser_clicked()
{
    User *n_u = new User();
    n_u->show();
    close();
}
